package com.logic.landseaserver.task;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.logic.landseaserver.common.LandeaConstants.CouponStatus;
import com.logic.landseaserver.persistence.write.CouponWriteMapper;
import com.logic.landseaserver.service.IScheduleService;

/**
 * 
 *
 * 项目名称：system-server</br>
 * 类名称：CouponTask</br>
 * 类描述：定时处理过期的授权码</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年10月18日 下午4:01:23</br>
 * @version 1.0
 *
 */
@Component
public class CouponTask
{
    
    private static final Logger LOG = LoggerFactory.getLogger(CouponTask.class);
    
    @Autowired
    private IScheduleService iScheduleService;
    
    @Autowired
    private CouponWriteMapper couponWriteMapper;
    
    
    /**
     * 每天0:10:00执行
     * 更新已失效的授权码
     */
    @Scheduled(cron = "0 10 0 * * ?")
    public void updateInvalidCoupon()
    {
        try
        {
            boolean cuteFlag = iScheduleService.cuteSchedule();
            if (cuteFlag)
            {
                LOG.info("updateInvalidCoupon|定时更新失效的授权码开始......");
                
                // 失效时间为当前天的23:59:59
                Calendar currentDate = Calendar.getInstance();
                currentDate.set(Calendar.HOUR_OF_DAY, 0);
                currentDate.set(Calendar.MINUTE, 0);
                currentDate.set(Calendar.SECOND, 0);
                
                // 更新失效的授权码,不更新已使用的、部分投放、已投放的
                Map<String,Object> paramMap = new HashMap<String,Object>();
                paramMap.put("endTime", currentDate.getTime());
                paramMap.put("status", CouponStatus.FAILURE);
                couponWriteMapper.updateInvalidCoupon(paramMap);
            }
        }
        catch (Exception e)
        {
            LOG.error("updateInvalidCoupon|更新失效的授权码定时任务失败.", e);
        }
        
    }
}